import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class HdfsDemo {

    FileSystem fs;
    Configuration conf;

    @Before
    public void begin() throws Exception {
        // 加载src目录下的配置文件
        conf = new Configuration();
        fs = FileSystem.get(conf);
    }

    @After
    public void end() {
        try {
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void mkdir() throws IOException {
        Path path = new Path("/tmp");
        fs.mkdirs(path);
    }

    @Test
    public void upload() throws IOException {
        Path path = new Path("/tmp/Iedis-2.43.zip");
        FSDataOutputStream outputStream = fs.create(path);
        FileUtils.copyFile(new File("/Users/hwangchan/Desktop/Iedis-2.43.zip"), outputStream);
    }

    @Test
    public void delete() throws IOException {
        Path path = new Path("/tmp/test");
        fs.delete(path, true);
    }

    @Test
    public void upload2() throws IOException {
        Path path = new Path("/tmp/seq");
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path, Text.class, Text.class);
        File file = new File("/Users/hwangchan/Desktop/Iedis-2.43.zip");
        for (File f : file.listFiles()) {
            writer.append(new Text(f.getName()), new Text(FileUtils.readFileToString(f)));
        }
    }

    @Test
    public void download2() throws Exception {
        Path path = new Path("/tmp/seq");
        SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf);
        Text key = new Text();
        Text value = new Text();
        while (reader.next(key, value)) {
            System.out.println(key);
            System.out.println(value);
            System.out.println("------------------");
        }
    }
}
